//
//  WelcomeController.swift
//  sina
//
//  Created by 江先铨 on 15/11/13.
//  Copyright © 2015年 jxqstatic. All rights reserved.


import UIKit
import SnapKit
import SDWebImage

class WelcomeController: UIViewController {

    override func loadView() {
        
        view = backImage
        
         welcomeLabel.alpha = 0
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
      }

    override func viewDidAppear(animated: Bool) { //不要在viewdidload 下显示动画 这种显示动画的效果不好
        
        super.viewDidAppear(animated)
        setAnimation()
        
    }
    
    private func setAnimation () {
    
      iconImage.snp_updateConstraints { (make) -> Void in
          make.bottom.equalTo(view.snp_bottom).offset(-(view.bounds.height - 180))
        }
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //将提前收集到的约束变化 进行更新
             self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                      //  print("ok")
                        //当数据加载完成之后就需要通知控制器去跳转到主控制器上
                        NSNotificationCenter.defaultCenter().postNotificationName(APPSwithRootViewController, object: nil)
                })
        }

    }
    
    private func setupUI() {
    
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        
        iconImage.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.centerX.equalTo(view.snp_centerX)
            make.height.equalTo(90)
            make.width.equalTo(90)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(20)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        iconImage.sd_setImageWithURL(ViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        
    }
    
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var welcomeLabel: UILabel = UILabel(title: (ViewModel().name ?? "") + "  欢迎归来", size: 19, color: UIColor.lightGrayColor())
//    {
//    
//        let l = UILabel()
//        l.text = (ViewModel().name ?? "") + "  欢迎归来"
//        l.textColor = UIColor.lightGrayColor()
//        l.alpha = 0
//        l.font = UIFont.systemFontOfSize(19)
//        l.sizeToFit()
//        return l
//    }()

}
